package com.ruoyi.repair.service.impl;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.repair.domain.PropertyRepairLog;
import com.ruoyi.repair.mapper.PropertyRepairLogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.repair.mapper.PropertyRepairMapper;
import com.ruoyi.repair.domain.PropertyRepair;
import com.ruoyi.repair.service.IPropertyRepairService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 报修单Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-10-27
 */
@Service
public class PropertyRepairServiceImpl implements IPropertyRepairService 
{
    @Autowired
    private PropertyRepairMapper propertyRepairMapper;

    @Autowired
    private PropertyRepairLogMapper propertyRepairLogMapper;

    /**
     * 查询报修单
     * 
     * @param repairId 报修单主键
     * @return 报修单
     */
    @Override
    public PropertyRepair selectPropertyRepairByRepairId(Long repairId)
    {
        return propertyRepairMapper.selectPropertyRepairByRepairId(repairId);
    }

    /**
     * 查询报修单列表
     * 
     * @param propertyRepair 报修单
     * @return 报修单
     */
    @Override
    public List<PropertyRepair> selectPropertyRepairList(PropertyRepair propertyRepair)
    {
        return propertyRepairMapper.selectPropertyRepairList(propertyRepair);
    }

    /**
     * 新增报修单
     * 
     * @param propertyRepair 报修单
     * @return 结果
     */
    @Override
    public int insertPropertyRepair(PropertyRepair propertyRepair)
    {
        propertyRepair.setCreateTime(DateUtils.getNowDate());
        return propertyRepairMapper.insertPropertyRepair(propertyRepair);
    }

    /**
     * 修改报修单
     * 
     * @param propertyRepair 报修单
     * @return 结果
     */
    @Override
    public int updatePropertyRepair(PropertyRepair propertyRepair)
    {
        return propertyRepairMapper.updatePropertyRepair(propertyRepair);
    }

    /**
     * 批量删除报修单
     * 
     * @param repairIds 需要删除的报修单主键
     * @return 结果
     */
    @Override
    public int deletePropertyRepairByRepairIds(Long[] repairIds)
    {
        return propertyRepairMapper.deletePropertyRepairByRepairIds(repairIds);
    }

    /**
     * 删除报修单信息
     * 
     * @param repairId 报修单主键
     * @return 结果
     */
    @Override
    public int deletePropertyRepairByRepairId(Long repairId)
    {
        return propertyRepairMapper.deletePropertyRepairByRepairId(repairId);
    }

    // 提交报修单
    @Override
    public Long submitRepair(PropertyRepair repair) {
        // 生成报修单号
        repair.setRepairNo("R" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")));
        repair.setStatus("SUBMITTED");
        repair.setCreateTime(new Date());
        propertyRepairMapper.insertPropertyRepair(repair);

        // 记录日志
        saveRepairLog(repair.getRepairId(), "SUBMITTED", "业主提交报修", repair.getUserId());

        // 推送消息给物业管理员
   //     messagePushService.pushToRole("PROPERTY_ADMIN", "新报修提醒","有新的报修单：" + repair.getDescription().substring(0, 20) + "...");
        return repair.getRepairId();
    }

    // 分配维修人员
    @Override
    @Transactional
    public void assignRepair(Long repairId, Long staffId) {
        PropertyRepair repair = propertyRepairMapper.selectPropertyRepairByRepairId(repairId);
        if (!"SUBMITTED".equals(repair.getStatus())) {
            throw new ServiceException("当前状态无法分配");
        }

        // 更新报修单
        repair.setStatus("ASSIGNED");
        repair.setStaffId(staffId);
        repair.setAssignTime(new Date());
        propertyRepairMapper.updatePropertyRepair(repair);

        // 记录日志
        saveRepairLog(repairId, "ASSIGNED", "分配给维修人员：" + staffId,
                SecurityUtils.getUserId());

        // 推送消息给维修人员
      //  messagePushService.pushToUser(staffId, "新维修任务", "您有新的维修任务，请及时处理");
    }

    // 完成维修
    @Override
    @Transactional
    public void completeRepair(Long repairId, BigDecimal cost, String content) {
        PropertyRepair repair = propertyRepairMapper.selectPropertyRepairByRepairId(repairId);
        if (!"PROCESSING".equals(repair.getStatus())) {
            throw new ServiceException("当前状态无法完成");
        }

        // 更新报修单
        repair.setStatus("COMPLETED");
        repair.setCompleteTime(new Date());
        repair.setCost(cost);
        propertyRepairMapper.updatePropertyRepair(repair);

        // 记录日志
        saveRepairLog(repairId, "COMPLETED", content, SecurityUtils.getUserId());

        // 推送消息给业主
     //   messagePushService.pushToUser(repair.getUserId(), "维修完成", "您的报修已完成，请查收");
    }

    // 保存维修日志
    private void saveRepairLog(Long repairId, String status, String content, Long operatorId) {
        PropertyRepairLog log = new PropertyRepairLog();
        log.setRepairId(repairId);
        log.setStatus(status);
        log.setContent(content);
        log.setOperatorId(operatorId);
        log.setOperateTime(new Date());
        propertyRepairLogMapper.insertPropertyRepairLog(log);
    }


}
